﻿using System;
using System.ComponentModel;

namespace _5E.Utils.EnumHelper.Extension
{
    /// <summary>
    /// 程序说明：枚举扩展
    /// 创建作者：ahl
    /// 创建时间：2012-03-25
    /// </summary>
    public static class EnumExtension
    {
        #region DescriptionAttribute

        /// <summary>
        /// 获取介绍
        /// </summary>
        /// <param name="tEnum">枚举值</param>
        /// <returns></returns>
        public static string GetDesc(this Enum tEnum)
        {
            DescriptionAttribute descriptionAttribute;
            return tEnum.TryGetAttribute(out descriptionAttribute) ? string.Empty : descriptionAttribute.Description;
        }

        #endregion

        #region 获取属性

        /// <summary>
        /// 尝试获取枚举的指定特性
        /// </summary>
        /// <typeparam name="TAttribute">指定特性类型</typeparam>
        /// <param name="tEnum">当前枚举</param>
        /// <param name="outAttribute">指定的特性</param>
        /// <returns>是否存在获取指定的特性</returns>
        public static bool TryGetAttribute<TAttribute>(this Enum tEnum, out TAttribute outAttribute)
            where TAttribute : Attribute
        {
            return tEnum.TryGetAttribute(out outAttribute, false);
        }

        /// <summary>
        /// 尝试获取枚举的指定特性
        /// </summary>
        /// <typeparam name="TAttribute">指定特性类型</typeparam>
        /// <param name="tEnum">当前枚举</param>
        /// <param name="outAttribute">指定的特性</param>
        /// <param name="inherit">是否查找继承链</param>
        /// <returns>是否存在获取指定的特性</returns>
        public static bool TryGetAttribute<TAttribute>(this Enum tEnum, out TAttribute outAttribute, bool inherit)
            where TAttribute : Attribute
        {
            outAttribute = default(TAttribute);
            TAttribute[] arrAttribute;
            if (tEnum.TryGetAttribute(out arrAttribute, inherit))
            {
                outAttribute = arrAttribute[0];
                return true;
            }
            return false;
        }

        /// <summary>
        /// 尝试获取枚举的指定特性
        /// </summary>
        /// <typeparam name="TAttribute">指定特性类型</typeparam>
        /// <param name="tEnum">当前枚举</param>
        /// <param name="outArrayAttribute">指定的特性</param>
        /// <returns>是否存在获取指定的特性</returns>
        public static bool TryGetAttribute<TAttribute>(this Enum tEnum, out TAttribute[] outArrayAttribute)
            where TAttribute : Attribute
        {
            return tEnum.TryGetAttribute(out outArrayAttribute, false);
        }

        /// <summary>
        /// 尝试获取枚举的指定特性
        /// </summary>
        /// <typeparam name="TAttribute">指定特性类型</typeparam>
        /// <param name="tEnum">当前枚举</param>
        /// <param name="outArrayAttribute">指定的特性</param>
        /// <param name="inherit">是否查找继承链</param>
        /// <returns>是否存在获取指定的特性</returns>
        public static bool TryGetAttribute<TAttribute>(this Enum tEnum, out TAttribute[] outArrayAttribute, bool inherit)
            where TAttribute : Attribute
        {
            var enumType = tEnum.GetType();
            var enumName = Enum.GetName(enumType, tEnum);
            var enumField = enumType.GetField(enumName);
            outArrayAttribute = null;
            var exists = false;
            if (enumField != null)
            {
                var arrAttr = enumField.GetCustomAttributes(typeof(TAttribute), inherit);
                if (0 < arrAttr.Length)
                {
                    exists = true;
                    outArrayAttribute = Array.ConvertAll(arrAttr, attr => attr as TAttribute);
                }
            }
            return exists;
        }

        #endregion
    }
}
